import QtQuick 2.0
import QtQuick.Controls 2.12

Item {
    id:root

    implicitWidth: 130
    implicitHeight: 60

    signal sClosed()

    Popup {
        id: popup
        parent: Overlay.overlay
        width: root.width
        height: root.height
        focus: true
        modal: true
        bottomPadding: 0
        x: Math.round((parent.width - width) / 2)
        y: Math.round((parent.height - height) / 2)
        background: Rectangle {
            anchors.fill: parent
            color: "#f0f0f0"
            radius: 10
        }

        Item {
            anchors.fill: parent
            Rectangle {
                width: parent.width
                height: parent.height - 30
                color: "#f0f0f0"
                TextEdit {
                    id:viewText
                    anchors.fill: parent
                    anchors.centerIn: parent
//                anchors.top: parent.top
//                anchors.horizontalCenter: parent.horizontalCenter
//                anchors.verticalCenter: parent.verticalCenter
//                    font.family: "Microsoft Yahei"
//                    color: "dimgrey"
                    font.family: msgText.font.family
                    color: msgText.color
                    text: msgText.text
                    wrapMode: Text.WordWrap
                    verticalAlignment: Text.AlignVCenter
    //                horizontalAlignment: Text.AlignHCenter
                    selectByMouse: true
                    readOnly: true
                }
            }

            Rectangle {
                width: parent.width
                height: 30
                color: "#f0f0f0"
                anchors.bottom: parent.bottom
                Rectangle {
                    anchors.centerIn: parent
                    implicitWidth: 50
                    implicitHeight: parent.height
                    color: "#f0f0f0"
                    Text {
                        anchors.centerIn: parent
                        font.family: msgText.font.family
                        color: "#888888"
                        text: qsTr("OK")
                    }
                    MouseArea {
                        anchors.fill:parent
                        acceptedButtons: Qt.LeftButton
                        onClicked: {
                            popup.close()
                        }
                    }
                }
            }
        }

        onClosed: {
            emit: sClosed()
        }
    }
    Text {
        id: msgText
        visible: false
//        verticalAlignment: Text.AlignVCenter
//        horizontalAlignment: Text.AlignHCenter
//        wrapMode: Text.WordWrap
        font.family: "Microsoft Yahei"
        color: "dimgrey"
    }
    function popupMessage(msg) {
        msgText.text = msg
        var vw = parent.width * 0.6;
        if( msgText.contentWidth >= vw) {
            root.width = vw + 24;
            var txtChars = msgText.contentWidth / msgText.font.pixelSize;
            var txtLineChars = vw / msgText.font.pixelSize;
            root.height = ( txtChars / txtLineChars + 1) * (msgText.font.pixelSize + 8) + 30;
        }
        else{
            root.width = msgText.contentWidth + 130;
            root.height = 60;
            viewText.horizontalAlignment = Text.AlignHCenter;
        }
        popup.open()
    }
}
